import http from "@/utils/http";
import type { MaterialAddParamsType, MaterialRequisitionParamsType } from "./params";
import type {
    MaterialAddResponseData,
    MaterialRequisitionItem,
    MaterialRequisitionResponseData,
} from "./res";
import { headerCommon, headerMaster } from "../header";

// 标准写法
enum API {
    GET_MATERIAL_LIST = "/1.1/classes/productPicking", // 查询生产领料单列表
    POST_ADD_MATERIAL = "/1.1/classes/productPicking", // 新增生产领料单
    GET_MATERIAL_DETAIL = "/1.1/classes/productPicking", // 获取领料单详情
    PUT_UPDATE_MATERIAL = "/1.1/classes/productPicking", // 更新领料单
    DELETE_MATERIAL = "/1.1/classes/productPicking", // 删除领料单
}

/**
 * 查询生产领料单列表
 * @param params 查询参数
 */
export const getMaterialRequisitionList = (data?: MaterialRequisitionParamsType, params?) =>
    http.get<any, MaterialRequisitionResponseData>(API.GET_MATERIAL_LIST + `${data ? `?where=${JSON.stringify(data)}` : ""}`, {
        headers: headerCommon,
        ...(params ? { params: { limit: params.pageSize, skip: (params.currentPage - 1) * params.pageSize } } : {})
    });

/**
 * 新增生产领料单
 * @param data 领料单数据
 */
export const addMaterialRequisition = (data: MaterialAddParamsType) => {
    return http.post<any, MaterialAddResponseData>(
        API.POST_ADD_MATERIAL,
        {
            ...data,
            status: '未出库', // 默认状态为待出库
        },
        {
            headers: headerMaster,
        }
    );
};

/**
 * 获取领料单详情
 * @param objectId 领料单唯一标识
 */
export const getMaterialRequisitionDetail = (objectId: string) => {
    return http.get<any, MaterialRequisitionItem>(`${API.GET_MATERIAL_DETAIL}/${objectId}`, {
        headers: headerCommon,
    });
};

/**
 * 更新领料单
 * @param objectId 领料单唯一标识
 * @param data 更新数据
 */
export const updateMaterialRequisition = (objectId: string, data: Partial<MaterialAddParamsType>) => {
    return http.put<any, { updatedAt: string }>(`${API.PUT_UPDATE_MATERIAL}/${objectId}`,
        data
        ,
        {
            headers: headerMaster,
        });
};

/**
 * 删除领料单
 * @param objectId 领料单唯一标识
 */
export const deleteMaterialRequisition = (objectId: string) => {
    return http.delete<any, {}>(`${API.DELETE_MATERIAL}/${objectId}`, {
        headers: headerMaster,
    });
};

/**
 * 执行出库操作
 * @param objectId 领料单唯一标识
 */
export const executeOutbound = (objectId: string) => {
    return http.put<any, { updatedAt: string }>(`${API.PUT_UPDATE_MATERIAL}/${objectId}`,
        {
            status: "已出库", // 更新为已出库状态
        },
        {
            headers: headerMaster,
        });
};

/**
 * 执行出库操作
 * @param objectId 领料单唯一标识
 */
export const executeInbound = (objectId: string) => {
    return http.put<any, { updatedAt: string }>(`${API.PUT_UPDATE_MATERIAL}/${objectId}`,
        {
            status: "未出库", // 更新为未出库状态
        },
        {
            headers: headerMaster,
        });
};
